<?php
/**

 * [Fengxu!] (C)2008-2009 SSBT Inc.
 * This is NOT a freeware, use is subject to license terms
 * $Id: syscommon.inc.php Mon Apr 07 02:16:31 GMT 2008 $
 * @author fengxu qq:250484464 <zhiming0824@163.com>
 * @version $Id 02:16:31$
 * @license fengxu

 */

error_reporting(E_ALL&~E_NOTICE&~E_WARNING);

define('MCC_ROOT',dirname(__FILE__));

define('IN_MCC',TRUE);

require_once MCC_ROOT.'/include/global.inc.php';

$timezoneoffset = 8;
$timezoneoffset=$timezoneoffset>0 ? '-'.$timezoneoffset : '+'.$timezoneoffset;
function_exists('date_default_timezone_set') && date_default_timezone_set("Etc/GMT$timezoneoffset");

$defined_vars = get_defined_vars();

foreach ($defined_vars as $_key => $_value)

{

	if(!in_array($_key,array('_POST','_GET','GLOBALS','_FILES','_SERVER','_COOKIE','_SESSION')))

	{

		${$_key} = '';

		unset(${$_key});

	}

}

$t_array = explode(' ',microtime());

$systime = $t_array[0]+$t_array[1];

$timestamp = time();

get_magic_quotes_gpc()? define('MAGIC_QUOTES_GPC',TRUE) : define('MAGIC_QUOTES_GPC',FALSE);

if(!MAGIC_QUOTES_GPC)

{

	addslashes_deep($_POST);

	addslashes_deep($_GET);

	addslashes_deep($_COOKIE);

}

addslashes_deep($_FILES);

if($_SERVER['HTTP_X_FORWARDED_FOR'])

{

	$onlineip = $_SERVER['HTTP_X_FORWARDED_FOR'];

	$c_agentip = 1;

}elseif ($_SERVER['HTTP_CLIENT_IP'])

{

	$onlineip = $_SERVER['HTTP_CLIENT_IP'];

	$c_agentip = 1;

}elseif ($_SERVER['REMOTE_ADDR'])

{

	$onlineip = $_SERVER['HTTP_CLIENT_IP'];

	$c_agentip = 0;

}

require_once MCC_ROOT.'/config.inc.php';

import_request_variables('pgc');

if($uptype=='bakupds')

{

	$charset = '';

	$dbcharset = '';

}

if(!defined('IN_MCC')) {
	exit('Access Denied');
}
class dbstuff {
	var $querynum = 0;
	var $link;
	function connect($dbhost, $dbuser, $dbpw, $dbname = '', $pconnect = 0, $halt = TRUE) {
		if($pconnect) {
			if(!$this->link = @mysql_pconnect($dbhost, $dbuser, $dbpw)) {
				$halt && $this->halt('Can not connect to MySQL server');
			}
		} else {
			if(!$this->link = @mysql_connect($dbhost, $dbuser, $dbpw, 1)) {
				$halt && $this->halt('Can not connect to MySQL server');
			}
		}

		if($this->version() > '4.1') {
			global $charset, $dbcharset;
			if(!$dbcharset && in_array(strtolower($charset), array('gbk', 'big5', 'utf-8'))) {
				$dbcharset = str_replace('-', '', $charset);
			}

			if($dbcharset) {
				@mysql_query("SET character_set_connection=$dbcharset, character_set_results=$dbcharset, character_set_client=binary", $this->link);
			}
			if($this->version() > '5.0.1') {
				@mysql_query("SET sql_mode=''", $this->link);
			}
		}

		if($dbname) {
			@mysql_select_db($dbname, $this->link);
		}

	}

	function select_db($dbname) {
		return mysql_select_db($dbname, $this->link);
	}

	function fetch_array($query, $result_type = MYSQL_ASSOC) {
		return mysql_fetch_array($query, $result_type);
	}

	function query($sql, $type = '') {
		global $debug, $discuz_starttime, $sqldebug, $sqlspenttimes;

		$func = $type == 'UNBUFFERED' && @function_exists('mysql_unbuffered_query') ?
			'mysql_unbuffered_query' : 'mysql_query';
		if(!($query = $func($sql, $this->link))) {
			if(in_array($this->errno(), array(2006, 2013)) && substr($type, 0, 5) != 'RETRY') {
				$this->close();
				require DISCUZ_ROOT.'./config.inc.php';
				$this->connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect);
				$this->query($sql, 'RETRY'.$type);
			} elseif($type != 'SILENT' && substr($type, 5) != 'SILENT') {
				$this->halt('MySQL Query Error', $sql);
			}
		}

		$this->querynum++;
		return $query;
	}

	function affected_rows() {
		return mysql_affected_rows($this->link);
	}

	function error() {
		return (($this->link) ? mysql_error($this->link) : mysql_error());
	}

	function errno() {
		return intval(($this->link) ? mysql_errno($this->link) : mysql_errno());
	}

	function result($query, $row) {
		$query = @mysql_result($query, $row);
		return $query;
	}

	function num_rows($query) {
		$query = mysql_num_rows($query);
		return $query;
	}

	function num_fields($query) {
		return mysql_num_fields($query);
	}

	function free_result($query) {
		return mysql_free_result($query);
	}

	function insert_id() {
		return ($id = mysql_insert_id($this->link)) >= 0 ? $id : $this->result($this->query("SELECT last_insert_id()"), 0);
	}

	function fetch_row($query) {
		$query = mysql_fetch_row($query);
		return $query;
	}

	function fetch_fields($query) {
		return mysql_fetch_field($query);
	}

	function version() {
		return mysql_get_server_info($this->link);
	}

	function close() {
		return mysql_close($this->link);
	}

	function halt($message = '', $sql = '') {
	}
}

$tracker = new dbstuff;
$tracker->connect($dsdbhost, $dsdbuser, $dsdbpw, $traname, $dspconnect);
$tra_db = $tracker;
//TrackerTactic数据库专用链接

$dsdb = new dbstuff;
$dsdb->connect($dsdbhost, $dsdbuser, $dsdbpw, $dsdbname, $dspconnect);
$dsid = $dsdb;
$db = new dbstuff;
$db->connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect);
$dp = new dbstuff;
$dp->connect($dphost, $dpuser, $dppw, $dpname, $dppconnect);
$cacti = new dbstuff;
$cacti->connect($dbhost, $dbuser, $dbpw, $cactidb, $pconnect);
$_DCOOKIE = $_DSESSION =  array();

$prelength = strlen($cookiepre);

foreach($_COOKIE as $key => $val) {

	if(substr($key, 0, $prelength) == $cookiepre) {

		$_DCOOKIE[(substr($key, $prelength))] = MAGIC_QUOTES_GPC ? $val : addslashes_deep($val);

	}

}//把COOKIE里面的值过滤后倒入

$MSYS_AUTH_KEY = md5($authkey.$_SERVER['HTTP_USER_AGENT']);

list($msys_pw, $msys_uid,$msys_time) = empty($_DCOOKIE['auth']) ? array('', '', 0) : addslashes_deep(explode("\t", authcode($_DCOOKIE['auth'], 'DECODE')));

if(is_numeric($msys_uid)&&strlen($msys_pw)>6)

{

	$sql = "SELECT * FROM msys_members WHERE uid='$msys_uid' AND password='$msys_pw'";

	$query = $db->query($sql);

	$qend = $db->fetch_array($query);

	if($qend)

	{

		$msys_user = $qend['username'];

		$userdb = array();

		$userdb['username'] = $qend['username'];

		$userdb['email'] = $qend['email'];

		$userdb['tentqq'] = $qend['tentqq'];

		$userdb['logindate'] = gmdate("Y-m-d H:i:s",$qend['logindate']);

		$userdb['level'] = $qend['level'];

		$userdb['allowoperation'] = explode(':',($qend['allowoperation']));

		$larr = explode(':',($qend['allowoperation']));

	}

	else 

	{

		$msys_pw = $msys_uid = $larr='';

	}

}

else 

{

	$msys_pw = $msys_uid = $larr='';

}

$PHP_SELF = $_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME'];

if(!$msys_uid&&!strstr($PHP_SELF,'login.php')&&!strstr($PHP_SELF,'msysck.php')&&!strstr($PHP_SELF,'sendpwd.php'))
{
	if(strstr($_SERVER['REQUEST_URI'],'cacti'))
	{
		adminmsg('没有登陆，没有权限进行操作！','../controlcc/login.php?action=login',1,1);
	}
	else 
	{
		adminmsg('没有登陆，没有权限进行操作！','login.php?action=login',1);
	}

}

include_once 'include/mysql_info.php';

$tracker = new dbstuff;
$tracker->connect($dsdbhost, $dsdbuser, $dsdbpw, $dsdbname, $dspconnect);
$tra_db = $tracker;
//TrackerTactic数据库专用链接
error_reporting(0);
//$url=$matches['scheme'].'://'.$matches['host'].'/';
if('edit'==$action){
	include_once MCC_ROOT.'/templates/msystem/mysqlconfig_error.htm';
}else{
	if(!$tra_db->query("select * from btc ")){
		adminmsg('数据库链接错误！','change_mysql.php?action=edit');
	}else{
		include_once MCC_ROOT.'/templates/msystem/mysqlconfig.htm';
	}
}
?>